//
//  UUOrderDetailAssignInfoVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 25/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderDetailAssignInfoVC.h"
#import "UUPermissionManager.h"
#import "UUResourceManager.h"
#import "UUOrderAssignVC.h"

@interface UUOrderDetailAssignItemView : UIView

@property (nonatomic, strong) UILabel *fleetLabel;
@property (nonatomic, strong) UILabel *carNumberLabel;
@property (nonatomic, strong) UILabel *driverLabel;

@end

@implementation UUOrderDetailAssignItemView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.fleetLabel = [UILabel new];
        self.fleetLabel.textColor = COMMON_TEXT_BLACK_COLOR;
        self.fleetLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.fleetLabel];
        [self.fleetLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.height.mas_equalTo(20);
        }];
        
        self.carNumberLabel = [UILabel new];
        self.carNumberLabel.textColor = self.fleetLabel.textColor;
        self.carNumberLabel.font = self.fleetLabel.font;
        [self addSubview:self.carNumberLabel];
        [self.carNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.height.equalTo(self.fleetLabel);
            make.top.equalTo(self.fleetLabel.mas_bottom).offset(9);
            make.bottom.mas_equalTo(0);
        }];
        
        self.driverLabel = [UILabel new];
        self.driverLabel.textColor = COMMON_TEXT_DARK_COLOR;
        self.driverLabel.textAlignment = NSTextAlignmentRight;
        self.driverLabel.font = self.fleetLabel.font;
        [self addSubview:self.driverLabel];
        [self.driverLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.height.left.right.equalTo(self.carNumberLabel);
        }];
    }
    return self;
}

@end

/////////////////////////////////////////////////////////////////

@interface UUOrderDetailAssignInfoVC ()

@end

@implementation UUOrderDetailAssignInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.order != nil);
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (self.order.status == UUOrderStatusCancelled && self.order.dispatch.count == 0) {
        return;
    }
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"车牌与司机";
    titleLabel.textColor = COMMON_TEXT_ORANGE_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(28);
        make.top.mas_equalTo(14);
        make.left.mas_equalTo(20);
    }];
    
    if (self.order.status == UUOrderStatusWaitingForService) {
        UIButton *modifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        modifyButton.titleLabel.font = titleLabel.font;
        [modifyButton setTitle:@"修改" forState:UIControlStateNormal];
        [modifyButton setTitleColor:COMMON_TEXT_LIGHT_COLOR forState:UIControlStateNormal];
        [modifyButton addTarget:self action:@selector(updateAssignInfo) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:modifyButton];
        [modifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(50);
            make.right.mas_equalTo(-20);
            make.top.bottom.equalTo(titleLabel);
        }];
        
        if (![GetPermissionManager() checkPermission:UUPermissionFlagOrderReadWrite]) {
            modifyButton.enabled = NO;
        }
    } else if (self.order.status == UUOrderStatusWaitingForAssign) {
        UIButton *modifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        modifyButton.titleLabel.font = titleLabel.font;
        [modifyButton setTitle:@"分配" forState:UIControlStateNormal];
        [modifyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [modifyButton setBackgroundImage:GetResourceManager().buttonImage2 forState:UIControlStateNormal];
        [modifyButton setBackgroundImage:GetResourceManager().buttonImage2HL forState:UIControlStateHighlighted];
        [modifyButton addTarget:self action:@selector(updateAssignInfo) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:modifyButton];
        [modifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(38);
            make.centerX.mas_equalTo(0);
            make.top.equalTo(titleLabel.mas_bottom).offset(20);
        }];
        
        if (![GetPermissionManager() checkPermission:UUPermissionFlagOrderReadWrite]) {
            modifyButton.enabled = NO;
        }
    }
    
    UIView *lastView = titleLabel;
    for (UUOrderDispatchItemModel *itemModel in self.order.dispatch) {
        UUOrderDetailAssignItemView *itemView = [UUOrderDetailAssignItemView new];
        itemView.fleetLabel.text = itemModel.fleetName;
        itemView.carNumberLabel.text = itemModel.carNum;
        
        itemView.driverLabel.text = [NSString stringWithFormat:@"%@ %@", itemModel.driverName, itemModel.driverMobile];
        
        [self.view addSubview:itemView];
        
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView.mas_bottom).offset(15);
            
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
        
        lastView = itemView;
    }
    
    [self.view.subviews.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-20);
    }];
    
    self.preferredContentSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
}

- (void)updateAssignInfo {
    UUOrderAssignVC *assignVC = [UUOrderAssignVC instanceOfAppropriate];
    assignVC.orderId = self.order.id;
    [self.navigationController pushViewController:assignVC animated:YES];

}

@end
